<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  confirm-text="完成"
  title="批量对应采集方式"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateConfirmModel"
  :confirm-loading="loadingMap.save"
  width="700"
)
  RadioGroup(
    v-model="category"
    :options="data.collectionMethodlistAsOptions"
    )
  </template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import { useNotify, RadioGroup } from "@scmt/common-components";
import { useRequest } from "@scmt/common-hooks";
import { type CombinedEntity } from "@scmt/api-lis-combined";
//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Array as PropType<CombinedEntity[]>,
    default: () => [],
  },
});
const $api = useApi();
const $notify = useNotify();
const editingObj = ref<{} | Object>({});

const appmodel = defineModel({
  default: false,
});
const category = ref("1");
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const { data, request, loadingMap } = useRequest({
  save: (q) => {
    return $api.lis.combined
      .batchCollectionMethod({
        combinedList: q.combinedList,
        collectionMethod: q.collectionMethod,
      })
      .then(() => {
        $notify.success("修改成功");
        emit("message-to-parent");
        appmodel.value = false;
      });
  },
  collectionMethodlistAsOptions: () => {
    return $api.dict.item.listAsOptions("collection_method");
  },
});

const updateConfirmModel = () => {
  if (!props.selectionRow.length) {
    $notify.warning("请选择合管");
    return;
  }
  request("save", {
    combinedList: props.selectionRow,
    collectionMethod: category.value,
  });
};

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      request("collectionMethodlistAsOptions");
      editingObj.value = {};
    }
  },
  {
    immediate: true,
  }
);
</script>
